<?php
/**
 * 班车订票消息模板
 * Created by PhpStorm
 * User: wujun
 * Date: 2017/05/15 10:26
 */
class BusBookingMsgtpl {

/***************************************************基本方法(BaseMsgtpl)***********************************************/

    protected $wxObj;       //微信API对象
    protected $wechatId;    //微信公众号id
    protected $wechatInfo;  //微信基础信息
    public $templateId;      //微信模版id
    public $accountId;      //账户id

    public function __construct($accountId, $wxTplSign = null)
    {

        $this->wechatInfo = $this->getWechatInfo($accountId); //获得微信基础信息
        $this->wechatId = $this->wechatInfo['wechat_id']; //微信公众号id

        if ($wxTplSign) { //如果在一个Action下只有一个模版标签,直接设置
            $this->templateId = $wxTplSign; //微信模版标识设置
        }

        if (empty($this->wxObj)) {    //微信对象
            $this->wxObj = $this->getWechatObj($this->wechatId);
        }

    }

    /**
     * 获得微信基础信息
     */
    private function getWechatInfo($accountId = 0)
    {
        $wechatInfo = M('Wechat')->where(array('account_id'=>$accountId))->find();
        return !empty($wechatInfo) ? $wechatInfo : null;
    }

    /**
     * 获得微信对象
     * @param int $wechatId 公众号对应的主键值
     */
    protected function getWechatObj($wechatId)
    {
        vendor('Wechat.WechatApi');
        $weixin = new WechatApi($wechatId);
        return $weixin;
    }

/************************************************发送消息方法(BusBookingMsgtpl)******************************************/

    /**
     * 获得微信模版id
     * $type integer 消息模板类型
     * @return string
     */
    public function getTemplateId($type = 1)
    {

        if (!$this->templateId) {
            switch ($type) {
                case 1 :
                    switch ($this->wechatInfo['account_id']) {
                        case 2  : $this->templateId = 'D3IHmxcLckGTHiXw-LDlApX9VIWdOvcZgVqyunmESjo'; break;
                        case 3  : $this->templateId = 'Xip28HCL4EbvNB6bItDLrIheQnOceLI87-ZDE5f4m0A'; break;
                        /*
                         * ... other accountId
                         *
                         * */
                        case 25 : $this->templateId = 'z45szT_MnjF9lvP7u_nS-GueZqgIDdyya_ofwkjNg6g'; break;
                        // 28 树人大学 正式服模板id
                        //case 28 : $this->templateId = '-o2OC55fvrdUsNuXd0wFsM2hO3Rn8ey277dCpJTg3JI'; break;
                        // 28 云感物联 测试服模板id 1
                        //case 28 : $this->templateId = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs'; break;
                        // 28 云感物联 测试服模板id 2
                        case 28 : $this->templateId = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk'; break;
                        // 38 中医药
                        case 38 : $this->templateId = '5IynHwLIR0Q5qfdU2csg5W61at6IXJzYALm15oag0yg'; break;
                        default : $this->templateId = null;
                    } break;
                case 2 :
                    switch ($this->wechatInfo['account_id']) {
                        case 2  : $this->templateId = 'p_P3GalanUy8syyholudeTR3OB5hzEuEAz-w12D_lbQ'; break;
                        case 3  : $this->templateId = 'DAxDdnfAhywT-lfRTsaSGOQwd96g5RQnXMTigQknWCA'; break;
                        /*
                         * ... other accountId
                         *
                         * */
                        case 25 : $this->templateId = 'RUdkrbYCT4fM8eYiEzMGoFrdj7tM2g9YI_Px4xaxETU'; break;
                        // 28 树人大学 正式服模板id
                        //case 28 : $this->templateId = '-o2OC55fvrdUsNuXd0wFsM2hO3Rn8ey277dCpJTg3JI'; break;
                        // 28 云感物联 测试服模板id 1
                        //case 28 : $this->templateId = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs'; break;
                        // 28 云感物联 测试服模板id 2
                        case 28 : $this->templateId = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk'; break;
                        // 38 中医药
                        case 38 : $this->templateId = 'QnX9UuUXby11P9yeP2jXSI53Xh7Aeie4mOoC5eKoWpw'; break;
                        default : $this->templateId = null;
                    } break;
                case 3 :
                    switch ($this->wechatInfo['account_id']) {
                        case 2  : $this->templateId = 'ervBp_BlUtHqKsaIw0qjmFz2KXy9J1tWCF5TIhW042Q'; break;
                        case 3  : $this->templateId = 'A4NBwXw1VQmYDOBzCImVstEnwLG2tvbkztK8vbJkc-I'; break;
                        /*
                         * ... other accountId
                         *
                         * */
                        case 25 : $this->templateId = 'z45szT_MnjF9lvP7u_nS-GueZqgIDdyya_ofwkjNg6g'; break;
                        // 28 树人大学 正式服模板id
                        //case 28 : $this->templateId = '-o2OC55fvrdUsNuXd0wFsM2hO3Rn8ey277dCpJTg3JI'; break;
                        // 28 云感物联 测试服模板id 1
                        //case 28 : $this->templateId = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs'; break;
                        // 28 云感物联 测试服模板id 2
                        case 28 : $this->templateId = '_hHj0eU1ZQ7yorFomzpQvFV21NxQj8S_bqolOO9sbBk'; break;
                        // 38 中医药
                        case 38 : $this->templateId = 'aGrSGJqqLVWqcyP7wfHa7iv-NyzMZFjzSnlobFaat2s'; break;
                        default : $this->templateId = null;
                    } break;
                default : $this->templateId = null;
            }
        }

        return $this->templateId;
    }

    /**
     * 支付成功发送消息给用户
     * @param array   $data	    订单信息
     * @param integer $userId	用户id
     * @return
     */
    public function sendUserMsg($data,$userId)
    {

        $userOpenid = M('WechatUser')->where(array('user_id'=>$userId))->getField('openid');
        $url = C('site_url').'/index.php?g=base&m=busBooking&a=orderList&apply_id='.$data['apply_id'].'&order_id='.$data['order_id'];
        $this->templateId = $this->getTemplateId(1);

        if ($data['account_id'] == 28) {
            $info = array(
                'first' => array("value" => '测试服班车购票通知','color' => '#173177'),
                'keyword1' => array("value" => '编号---'.$data['order_sn'].'金额---'.$data['order_amount'].'数量---'.$data['goods_number'] ),
                'remark' => array("value" => "请点击查看！","color" => "#173177"),
            );
        } elseif($data['account_id'] == 38) {
            $info = array(
                'first' => array("value" => '恭喜您购票成功！','color' => '#173177'),
                'type' => array("value" => '活动' ),
                'e_title' => array("value" => '中医药班车订票' ),
                'o_id' => array("value" => $data['order_sn'] ),
                'o_money' => array("value" => $data['order_amount'] ),
                't_count' => array("value" => $data['goods_number'] ),
                'remark' => array("value" => "点击查看详情","color" => "#173177"),
            );
        } else {
            $info = array(
                'first' => array("value" => '购票成功通知','color' => '#173177'),
                'keyword1' => array("value" => $data['order_sn'] ),
                'keyword2' => array("value" => $data['order_amount'] ),
                'keyword3' => array("value" => $data['goods_number'] ),
                'remark' => array("value" => "请点击查看详情","color" => "#173177"),
            );
        }

        $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $userOpenid, $this->templateId, $info, $url);
        return $result;
    }

    /**
     * 发送班次消息给检票员
     * @param array   $data	    订单信息
     * @param integer $userId	用户id
     * @return
     */
    public function sendCheckerMsg($data,$userId)
    {
        $userOpenid = M('WechatUser')->where(array('user_id'=>$userId))->getField('openid');
        $url = C('site_url').'/index.php?g=base&m=BusBookingCheck&a=ticketsList&apply_id='.$data['apply_id'];
        $this->templateId = $this->getTemplateId(3);

        if ($this->wechatInfo['account_id'] == 28) {
            $info = array(
                'first' => array("value" => '测试服班车购票结果通知','color' => '#173177'),
                'keyword1' => array("value" => '班次---'.$data['departure_time'].'  '.$data['start_address'].' - '.$data['end_address'].'日期---'.date('Y-m-d').'人数---'.$data['passenger_count'] ),
                'remark' => array("value" => "请点击查看！","color" => "#173177"),
            );
        } elseif($this->wechatInfo['account_id'] == 38) {
            $info = array(
                'first' => array("value" => '当前购票班次为： '.$data['departure_time'].'  '.$data['start_address'].' - '.$data['end_address'],'color' => '#173177'),
                'keyword1' => array("value" => date('Y-m-d') ),
                'keyword2' => array("value" => $data['passenger_count'] ),
                'remark' => array("value" => "请及时安排调度车辆！","color" => "#173177"),
            );
        } else {
            $info = array(
                'first' => array("value" => '购票结果通知','color' => '#173177'),
                'keyword1' => array("value" => $data['departure_time'].'  '.$data['start_address'].' - '.$data['end_address'] ),
                'keyword2' => array("value" => $data['passenger_count'] ),
                'remark' => array("value" => "请点击查看详情","color" => "#173177"),
            );
        }

        $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $userOpenid, $this->templateId, $info, $url);
        return $result;
    }


    /**
     * 检票成功发送消息给用户
     * @param array   $data	    订单信息
     * @param integer $userId	用户id
     * @return
     */
    public function sendFinishMsg($data,$userId)
    {
        $userOpenid = M('WechatUser')->where(array('user_id'=>$userId))->getField('openid');
        $url = C('site_url').'/index.php?g=base&m=busBooking&a=orderList&apply_id='.$data['apply_id'].'&order_id='.$data['order_id'];
        $this->templateId = $this->getTemplateId(3);

        if ($this->wechatInfo['account_id'] == 28) {
            $info = array(
                'first' => array("value" => '测试服班车检票成功通知','color' => '#173177'),
                'keyword1' => array("value" => '班次---'.$data['start_address'].' - '.$data['end_address'].'  '.date('Y-m-d').'  '.$data['departure_time'].'检票日期---'.date('Y-m-d H:i:s',$data['check_time']).'订单编号---'.$data['order_sn'] ),
                'remark' => array("value" => "请点击查看！","color" => "#173177"),
            );
        } elseif($this->wechatInfo['account_id'] == 38) {
            $info = array(
                'first' => array("value" => '尊敬的乘客，您的车票已检票成功！','color' => '#173177'),
                'keyword1' => array("value" => $data['order_sn'] ),
                'keyword2' => array("value" => $data['start_address'].' - '.$data['end_address'].'  '.date('Y-m-d').'  '.$data['departure_time'] ),
                'keyword3' => array("value" => '无' ),
                'keyword4' => array("value" => date('Y-m-d H:i:s',$data['check_time']) ),
                'remark' => array("value" => "祝您旅途愉快！","color" => "#173177"),
            );
        } else {
            $info = array(
                'first' => array("value" => '检票成功通知','color' => '#173177'),
                'keyword1' => array("value" => $data['order_sn'] ),
                'keyword2' => array("value" => $data['start_address'].' - '.$data['end_address'].'  '.date('Y-m-d').'  '.$data['departure_time'] ),
                'keyword3' => array("value" => date('Y-m-d H:i:s',$data['check_time']) ),
                'remark' => array("value" => "请点击查看详情","color" => "#173177"),
            );
        }

        $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $userOpenid, $this->templateId, $info, $url);
        return $result;
    }


}
